package com.kefu.admin.utils;


import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

/**
 * 拼音工具类
 * 
 * @author 劉俊瑋
 */
public class PingYinUtil {

	public static String getAllPy(String chinese, boolean toUpperCase) {
		try {
			StringBuilder pinyin = new StringBuilder();
			for (char c : chinese.toCharArray()) {
				HanyuPinyinOutputFormat hanyuPinyinOutputFormat = new HanyuPinyinOutputFormat();
				hanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
				String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c,hanyuPinyinOutputFormat);
				if (pinyinArray != null && pinyinArray.length > 0) {
					pinyin.append(pinyinArray[0]);
				} else {
					pinyin.append(c);
				}
			}
			return pinyin.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static void main(String[] args) {
		System.out.println(getAllPy("行走", true));

	}
}